﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using clf.weighing.entity;
using Brushes = System.Windows.Media.Brushes;
using Image = System.Windows.Controls.Image;

namespace weighing
{
    /// <summary>
    /// BillImageUC.xaml 的交互逻辑
    /// </summary>
    public partial class BillImageUC : System.Windows.Controls.UserControl
    {
        private WeighingBill? mWeighingBill;
        private List<BillImage>? mImages;
        private long upClickTimes = 0;
    
        public BillImageUC(WeighingBill? bill)
        {
            InitializeComponent();

            mWeighingBill = bill;
        }
        #region loaded unloaded
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
            ShowIamge();
        }
        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            mWeighingBill = null;
            mImages = null;           
            this.ImgPanel.Children.Clear();
        }
        #endregion

        private void RefreshBtn_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
            ShowIamge();
        }

        private void LoadData()
        {
           // mImages = BillImageDao.Instance.SelectByBillNumber(mWeighingBill?.number??"");
            mImages = BillImageDao.Instance.SelectByBillId(mWeighingBill?.id??0);
        }


        private void ShowIamge()
        {
            this.ImgPanel.Children.Clear();
            if (mImages == null || mImages.Count == 0 || (mImages.Count == 1 && mImages[0].positon == 100))
            {
                //过磅时未添加摄像头或者截图失败，再或者图片被删除
                var tip = LangHelper.Get(LangKeyNames.ImageNotFindTip);
                var tb = new TextBlock()
                {
                    Text = LangHelper.Get(LangKeyNames.ImageNotFind),
                    Foreground = Brushes.Red,
                    Margin = new Thickness(10, 4, 10, 30),
                    ToolTip = tip,
                    HorizontalAlignment =System.Windows.HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                };
                ImgPanel.Children.Add(tb);
            }
            else
            {
                for (int i = 0; i < mImages.Count; i++)
                {
                    var item = mImages[i];
                    if(item.positon == 100) return;
                    string path = item.localPath.IsNullOrEmpty()?item.remoteUrl:item.localPath;
                    if (path.IsNullOrEmpty())
                    {
                        //磅单图片路径错误                        
                        ConsoleHelper.writeLine(LangHelper.Get(LangKeyNames.ImagePathError));
                        continue;
                    }
                    if (FileHelper.Exists(path))
                    {
                        var image = new Image
                        {
                            Name = "image_" + i,
                            Margin = new Thickness(2),
                            Source = BitmapHelper.FromString(path),
                            ToolTip = @path,
                            MaxWidth = 380,
                            Tag = item,
                        };
                        image.MouseLeftButtonUp += Image_MouseLeftButtonUp;
                        this.ImgPanel.Children.Add(image);
                    }
                    else
                    {
                        var t = LangHelper.Get(LangKeyNames.FileNotExistColon);
                        TextBlock tb = new TextBlock() {
                            Text = t + $"\n {path}",
                            HorizontalAlignment =System.Windows.HorizontalAlignment.Center,
                            VerticalAlignment = VerticalAlignment.Center,
                            Foreground = Brushes.MediumVioletRed,
                            MaxWidth = 380,
                            Padding = new Thickness(20,0,20,0),
                            TextWrapping = TextWrapping.WrapWithOverflow,
                            FontSize = 12
                        };
                        this.ImgPanel.Children.Add(tb);
                    }
                   
                }
            }
        }


        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            DateTime dateTime = DateTime.Now;
            long now = dateTime.Minute * dateTime.Millisecond;
            long diff = now - upClickTimes;
            if (diff > 1000)
            {
                upClickTimes = now;
                return;
            }
            else
            {
                var img = sender as Image;
                new BigImageWindow(img.Source,img.Tag as BillImage) {Owner = App.Current.MainWindow }.Show();
                upClickTimes = now;
            }
        }

 
    }
}
